home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / Sample Code / AppsToGo / Kibitz / Setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-24  |  7.6 KB  |  283 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        setup.c
  5. ** Written by:  Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved. */
  9.  
  10.  
  11.  
  12. /*****************************************************************************/
  13.  
  14.  
  15.  
  16. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  17. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  18. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  19.  
  20. #ifndef __GWLAYERS__
  21. #include <GWLayers.h>
  22. #endif
  23.  
  24. #ifndef __RESOURCES__
  25. #include <Resources.h>
  26. #endif
  27.  
  28. #ifndef __TOOLUTILS__
  29. #include <ToolUtils.h>
  30. #endif
  31.  
  32.  
  33. extern Cursor    *gCurrentCursor;
  34.  
  35.  
  36.  
  37.  
  38. /*****************************************************************************/
  39. /*****************************************************************************/
  40.  
  41.  
  42.  
  43. #pragma segment Config
  44. void    DoArrangeBoard(FileRecHndl frHndl, EventRecord *event, Point clickLoc)
  45. {
  46.     short            piece, fromSq, fromRow, fromCol, toRow, toCol, toSq;
  47.     short            palettePiece, color, delta, middle, i;
  48.     Boolean            squareWasEmpty, changedPalettePiece;
  49.     Rect            paletteRect, boardRect, fromRect;
  50.     Point            releaseLoc;
  51.     ControlHandle    ctl;
  52.  
  53.     paletteRect = PaletteRect();
  54.     if (PtInRect(clickLoc, &paletteRect)) {
  55.         palettePiece = 1 + (clickLoc.h - paletteRect.left) / kBoardSqSize;
  56.         if (clickLoc.v < paletteRect.top + kBoardSqSize)
  57.             palettePiece = -palettePiece;
  58.         (*frHndl)->doc.palettePiece = palettePiece;
  59.         DrawPalette(frHndl);
  60.         SetOrigin(0, 0);
  61.         return;
  62.     }
  63.  
  64.     SetOrigin(0, 0);
  65.     clickLoc.h += 4096;
  66.  
  67.     squareWasEmpty = false;
  68.  
  69.     boardRect = BoardRect();
  70.     if (PtInRect(clickLoc, &boardRect)) {
  71.  
  72.         fromRow = (clickLoc.v - kBoardVOffset) / kBoardSqSize;
  73.         fromCol = (clickLoc.h - kBoardHOffset) / kBoardSqSize;
  74.         fromRect.top    = 1 + fromRow * kBoardSqSize;
  75.         fromRect.left   = 1 + fromCol * kBoardSqSize;
  76.         fromRect.bottom = fromRect.top  + 32;
  77.         fromRect.right  = fromRect.left + 32;
  78.  
  79.         if ((*frHndl)->doc.invertBoard) {
  80.             fromRow = 7 - fromRow;
  81.             fromCol = 7 - fromCol;
  82.         }
  83.         fromSq = START_IBNDS + 10 * fromRow + fromCol;
  84.  
  85.         piece = (*frHndl)->doc.palettePiece;
  86.         changedPalettePiece = false;
  87.         if (event->modifiers & optionKey) {
  88.             changedPalettePiece = true;
  89.             i = (*frHndl)->doc.theBoard[fromSq];
  90.             if ((i != WK) && (i != BK) && (i != EMPTY)) {
  91.                 piece = (*frHndl)->doc.palettePiece = i;
  92.                 SetOrigin((kArrangeBoard * 4096), 0);
  93.                 DrawPalette(frHndl);
  94.                 SetOrigin(0, 0);
  95.             }
  96.         }
  97.  
  98.         if ((*frHndl)->doc.theBoard[fromSq] == EMPTY) {
  99.             squareWasEmpty = true;
  100.             if (piece == WP) {
  101.                 if (fromRow == 7) return;
  102.                 if (fromRow == 0) piece = WQ;
  103.             }
  104.             if (piece == BP) {
  105.                 if (fromRow == 0) return;
  106.                 if (fromRow == 7) piece = BQ;
  107.             }
  108.             (*frHndl)->doc.theBoard[fromSq]  = piece;
  109.             (*frHndl)->doc.gameIndex      = 0;
  110.             (*frHndl)->doc.numGameMoves   = 0;
  111.             (*frHndl)->fileState.docDirty = true;
  112.             ctl = (*frHndl)->doc.gameSlider;
  113.             (*ctl)->contrlMax   = 0;
  114.             (*ctl)->contrlValue = 0;
  115.  
  116.             if (fromSq == 21) (*frHndl)->doc.king[BLACK].rookMoves[QSIDE] = 0;
  117.             if (fromSq == 28) (*frHndl)->doc.king[BLACK].rookMoves[KSIDE] = 0;
  118.             if (fromSq == 91) (*frHndl)->doc.king[WHITE].rookMoves[QSIDE] = 0;
  119.             if (fromSq == 98) (*frHndl)->doc.king[WHITE].rookMoves[KSIDE] = 0;
  120.                 /* Adjust castling privileges. */
  121.  
  122.             (*frHndl)->doc.enPasMove    = (*frHndl)->doc.arngEnPasMove    = 0;
  123.             (*frHndl)->doc.enPasPawnLoc = (*frHndl)->doc.arngEnPasPawnLoc = 0;
  124.                 /* Double-pawn-push must be last change to enable en-passant. */
  125.         }
  126.  
  127.         SetCursor(*GetCursor(closedHandCursor));
  128.         gCurrentCursor = nil;
  129.  
  130.         piece = (*frHndl)->doc.theBoard[fromSq];
  131.         releaseLoc.h = releaseLoc.v = 0x4000;
  132.         if (piece) MoveThePiece(frHndl, fromSq, fromRect, clickLoc, &releaseLoc);
  133.  
  134.         if ((toRow = releaseLoc.v - kBoardVOffset) < 0) toRow = -1;
  135.         else                                            toRow /= kBoardSqSize;
  136.         if ((toCol = releaseLoc.h - kBoardHOffset) < 0) toCol = -1;
  137.         else                                            toCol /= kBoardSqSize;
  138.         if ((*frHndl)->doc.invertBoard) {
  139.             toRow = 7 - toRow;
  140.             toCol = 7 - toCol;
  141.         }
  142.         if ((toRow > -1) && (toRow < 8) && (toCol > -1) && (toCol < 8)) {
  143.             toSq = START_IBNDS + 10 * toRow + toCol;
  144.             if (toSq == fromSq) {
  145.                 if (!squareWasEmpty) {
  146.                     if (!changedPalettePiece) {
  147.                         if ((piece != WK) && (piece != BK)) {
  148.                             (*frHndl)->doc.theBoard[fromSq] = EMPTY;
  149.                             SetOrigin((kArrangeBoard * 4096), 0);
  150.                             DrawPalette(frHndl);
  151.                             SetOrigin(0, 0);
  152.                         }
  153.                     }
  154.                 }
  155.                 ImageDocument(frHndl, true);
  156.                 return;
  157.             }
  158.         }
  159.         else toSq = fromSq;
  160.  
  161.         if (((*frHndl)->doc.theBoard[toSq] == BK) || ((*frHndl)->doc.theBoard[toSq] == WK)) {
  162.             ImageDocument(frHndl, true);
  163.             return;
  164.         }
  165.  
  166.         piece = (*frHndl)->doc.theBoard[fromSq];
  167.         if ((piece == WP) && (toSq >= 91)) {
  168.             ImageDocument(frHndl, true);
  169.             return;
  170.         }
  171.         if ((piece == WP) && (toSq <= 28)) piece = WQ;
  172.         if ((piece == BP) && (toSq <= 28)) {
  173.             ImageDocument(frHndl, true);
  174.             return;
  175.         }
  176.         if ((piece == BP) && (toSq >= 91)) piece = BQ;
  177.  
  178.         (*frHndl)->doc.theBoard[toSq]    = piece;
  179.         (*frHndl)->doc.theBoard[fromSq]  = EMPTY;
  180.         (*frHndl)->doc.gameIndex      = 0;
  181.         (*frHndl)->doc.numGameMoves   = 0;
  182.         (*frHndl)->fileState.docDirty = true;
  183.         ctl = (*frHndl)->doc.gameSlider;
  184.         (*ctl)->contrlMax   = 0;
  185.         (*ctl)->contrlValue = 0;
  186.  
  187.         if ((piece == WK) || (piece == BK)) {
  188.             color = (piece < 0) ? WHITE : BLACK;
  189.             (*frHndl)->doc.king[color].kingLoc   = toSq;
  190.             (*frHndl)->doc.king[color].kingMoves = true;
  191.         }
  192.  
  193.         if ((fromSq==21) || (toSq==21)) ++(*frHndl)->doc.king[BLACK].rookMoves[QSIDE];
  194.         if ((fromSq==28) || (toSq==28)) ++(*frHndl)->doc.king[BLACK].rookMoves[KSIDE];
  195.         if ((fromSq==91) || (toSq==91)) ++(*frHndl)->doc.king[WHITE].rookMoves[QSIDE];
  196.         if ((fromSq==98) || (toSq==98)) ++(*frHndl)->doc.king[WHITE].rookMoves[KSIDE];
  197.             /* Adjust castling privileges. */
  198.  
  199.         (*frHndl)->doc.enPasMove    = (*frHndl)->doc.arngEnPasMove    = 0;
  200.         (*frHndl)->doc.enPasPawnLoc = (*frHndl)->doc.arngEnPasPawnLoc = 0;
  201.             /* Double-pawn-push must be last change to enable en-passant. */
  202.  
  203.         delta = 0;
  204.         if (piece == WP)
  205.             if ((fromSq >= 81) && (fromSq <= 88))
  206.                 delta = fromSq - toSq;
  207.         if (piece == BP)
  208.             if ((fromSq >= 31) && (fromSq <= 38))
  209.                 delta = toSq - fromSq;
  210.         if (delta == 20) {
  211.             middle = (fromSq + toSq) / 2;
  212.             if (!(*frHndl)->doc.theBoard[middle]) {
  213.                 (*frHndl)->doc.enPasMove        = middle;
  214.                 (*frHndl)->doc.arngEnPasMove    = middle;
  215.                 (*frHndl)->doc.enPasPawnLoc     = toSq;
  216.                 (*frHndl)->doc.arngEnPasPawnLoc = toSq;
  217.             }
  218.         }
  219.  
  220.         ImageDocument(frHndl, true);
  221.     }
  222. }
  223.  
  224.  
  225.  
  226. /*****************************************************************************/
  227.  
  228.  
  229.  
  230. #pragma segment Config
  231. void    DrawPalette(FileRecHndl frHndl)
  232. {
  233.     GrafPtr        curPort;
  234.     short        rr, cc, piece;
  235.     Rect        paletteRect;
  236.     CIconHandle    pieceIcon;
  237.  
  238.     GetPort(&curPort);
  239.     if (curPort->portRect.left != -4096) return;
  240.  
  241.     PenSize(1, 1);
  242.     for (rr = 1; rr < 6; ++rr) {
  243.         for (cc = -1; cc < 3; cc += 2) {
  244.             paletteRect = PaletteRect();
  245.             paletteRect.left += (rr - 1) * kBoardSqSize;
  246.             if (cc == 1) paletteRect.top += kBoardSqSize;
  247.             paletteRect.right  = paletteRect.left + 32;
  248.             paletteRect.bottom = paletteRect.top + 32;
  249.             piece = rr * cc;
  250.             if (piece == (*frHndl)->doc.palettePiece) piece += 13;
  251.             pieceIcon = ReadCIcon(263 + piece);
  252.             DrawCIconNoMask(pieceIcon, paletteRect);
  253.             InsetRect(&paletteRect, -1, -1);
  254.             FrameRect(&paletteRect);
  255.             KillCIcon(pieceIcon);
  256.         }
  257.     }
  258.     PenNormal();
  259. }
  260.  
  261.  
  262.  
  263. /*****************************************************************************/
  264.  
  265.  
  266.  
  267. #pragma segment Config
  268. Rect    PaletteRect(void)
  269. {
  270.     Rect    paletteRect;
  271.  
  272.     paletteRect.top    = 74;
  273.     paletteRect.left   = kBoardWidth + 21;
  274.     paletteRect.right  = paletteRect.left + 5 * kBoardSqSize;
  275.     paletteRect.bottom = paletteRect.top + 2 * kBoardSqSize;
  276.     OffsetRect(&paletteRect, -4096, 0);
  277.  
  278.     return(paletteRect);
  279. }
  280.  
  281.  
  282.  
  283.